package com.twotiger.and.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import app.twotiger.p2p.R;
import app.twotiger.p2p.R.color;

import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.bean.Coupon;
import com.twotiger.and.util.DateUtil;
import com.twotiger.and.util.StringUtils;

public class SelectCouponAdapter extends BaseAdapter {

    private BaseActivity context;
    private ArrayList<Coupon> nlist; //不可用集合
    private ArrayList<Coupon> alllist;
    private ArrayList<Coupon> list; //可用集合

    public SelectCouponAdapter(Context context, ArrayList<Coupon> list, ArrayList<Coupon> nlist) {
        super();
        this.context = (BaseActivity) context;
        this.list = list;
        this.nlist = nlist;
        alllist = new ArrayList<Coupon>();
        alllist.addAll(list);
        alllist.addAll(nlist);
    }

    @Override
    public int getCount() {
        System.out.println(alllist.size() + "----可用:" + list.size() + "-----不可用:" + nlist.size());
        return alllist.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int mposition = position;
        if (convertView == null) {
            LayoutInflater minflater = LayoutInflater.from(context);
            convertView = minflater.inflate(R.layout.inverst_coupon_layout, null);
        }
        LinearLayout coupon_canuse = ViewHolder.get(convertView, R.id.coupon_canuse);
        TextView coupon_type = ViewHolder.get(convertView, R.id.coupon_type);
        TextView expect_income = ViewHolder.get(convertView, R.id.expect_income);
        TextView timeout_txt = ViewHolder.get(convertView, R.id.timeout_txt);
        TextView tv_coupon_canuse = ViewHolder.get(convertView, R.id.tv_coupon_canuse);
        RelativeLayout rl_backgroud = ViewHolder.get(convertView, R.id.rl_backgroud);
        TextView coupon_val_suffix = ViewHolder.get(convertView, R.id.coupon_val_suffix);
        TextView coupon_val = ViewHolder.get(convertView, R.id.coupon_val);
        TextView tv_condition = ViewHolder.get(convertView, R.id.tv_condition);
        TextView coupon_name = ViewHolder.get(convertView, R.id.coupon_name);
        if (mposition == 0) {
            coupon_canuse.setVisibility(View.VISIBLE);
        } else if (mposition == list.size()) {
            coupon_canuse.setVisibility(View.VISIBLE);
            tv_coupon_canuse.setText("不可用优惠券:");
        } else {
            coupon_canuse.setVisibility(View.GONE);
        }
        if (list.size() == 0) {
            tv_coupon_canuse.setText("不可用优惠券:");
        }

        final Coupon coupon = alllist.get(mposition);
        if (mposition >= list.size()) {
            expect_income.setText("");
            rl_backgroud.setBackgroundResource(R.drawable.youhuiquan_cannotuse);
            int color = context.getResources().getColor(app.twotiger.p2p.R.color.color_gray);
            coupon_type.setTextColor(color);
            timeout_txt.setTextColor(color);
            coupon_val.setTextColor(color);
        } else {
            switch (coupon.getAwardType()) {
            case 1: //加息券
                expect_income.setText("预计额外可获得：" + coupon.getAddInterest() + "元");
                rl_backgroud.setBackgroundResource(R.drawable.jiaxiquan_canuse);
                int color2 = context.getResources().getColor(color.coupon_jiaxi_touzi);
                coupon_type.setTextColor(color2);
                timeout_txt.setTextColor(color2);
                coupon_val.setTextColor(color2);
                break;
            case 4: //代金券
                expect_income.setText("可抵扣" + coupon.getAwardValue() + "元");
                rl_backgroud.setBackgroundResource(R.drawable.daijinquan_canuse);
                int color3 = context.getResources().getColor(color.coupon_daijin_touzi);
                coupon_type.setTextColor(color3);
                timeout_txt.setTextColor(color3);
                coupon_val.setTextColor(color3);
                break;
            default:
                break;
            }
        }
        coupon_name.setText(coupon.getName());
        coupon_type.setText(coupon.getAwardTypeName());
        coupon_val.setText(coupon.getAwardValue() + "");
        coupon_val_suffix.setText(coupon.getAwardValueSuffix());
        timeout_txt.setText("有效期至"
                            + DateUtil.getDateStringForLong(Long.valueOf(coupon.getInvalidTime()),
                                DateUtil.DATE_FMT));
        tv_condition.setText(StringUtils.subString(coupon.getUseConditionDesc(), 12));
        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mposition >= list.size()) {
                    return;
                }
                Intent intent = new Intent().putExtra("COUPON", coupon);
                context.setResult(context.RESULT_OK, intent);
                context.finish();
                context.overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
            }
        });
        return convertView;
    }

}
